<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\helpers\traits\ImageUpload;
use app\helpers\traits\SearchDataForModel;
use app\model\Carousel;
use app\model\Notice;
use think\Request;

class System extends BaseController
{
    use SearchDataForModel, ImageUpload;

    /**
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 14:35
     * @return \think\response\Json
     * 保存轮播图
     */
    public function saveCarouselFile()
    {
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        if (intval($id) === 0) {
            Carousel::create($data);
        } else {
            $carousel = Carousel::find($id);
            $carousel->save($data);
        }
        return $this->response();
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 14:41
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 获取列表
     */
    public function getCarouselList()
    {
        $limit = $this->request->param('limit', 3);
        $res = $this->search(new Carousel(), [], $limit);
        return $this->response($res);
    }

    /**
     * @param $id
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 15:24
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除轮播
     */
    public function DelCarousel($id)
    {
        $ids = explode(',', $id);
        foreach ($ids as $id) {
            $img = Carousel::find($id);
            if (is_null($img)) {
                return $this->response(404, '此图片不存在');
            }
            $img->delete();
        }
        return $this->response();
    }


    /**
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 15:32
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存公告
     */
    public function saveNotice(){
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        if (intval($id) === 0) {
            Notice::create($data);
        } else {
            $notice = Notice::find($id);
            $notice->save($data);
        }
        return $this->response();
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 15:35
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 列表
     */
    public function getNoticeList()
    {
        $limit = $this->request->param('limit', 3);
        $res = $this->search(new Notice(), [], $limit);
        return $this->response($res);
    }

    /**
     * @param $id
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 15:37
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除公告
     */
    public function DelNotice($id)
    {
        $ids = explode(',', $id);
        foreach ($ids as $id) {
            $img = Notice::find($id);
            if (is_null($img)) {
                return $this->response(404, '此公告不存在');
            }
            $img->delete();
        }
        return $this->response();
    }



}
